<template>
  <div>
    <h1>Login</h1>
    <el-form ref="loginForm" :model="loginForm" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="验证码">
        <el-input v-model="loginForm.captcha" placeholder="请输入验证码" style="width: 150px"></el-input>
        <img :src="captchaImage" alt="验证码" style="cursor: pointer; margin-left: 10px">
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getLogin">登录</el-button>
        
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { captcha } from "@/api/role";
import { Login } from "@/api/role";
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
        captcha: "",
        checkKey: "", // 用于存储验证码的 checkKey
         remember_me: true,
      },
      captchaImage: "", // 用于存储验证码图片的 Base64 字符串
      timestamp:new Date().getTime(),
      sjc:parseInt(new Date().getTime()/1000),
    };
  },

  methods: {
    // 点击登录
   
     async getLogin() {
      this.loginForm.checkKey=this.timestamp;
      const { meta,data } = await Login(this.loginForm);
      
     if(meta.status == 200){
       console.log(data);
      
        this.$message.success("登录成功");
        this.$router.push("../form/index.vue");
     }
     if(meta.status!= 200)
            {return this.$message.error(meta.msg);}
          this.$store.dispatch('login',data.token)
        this.$message.error("登录失败");
      
    },
    // 获取验证码
   async getCode11() {
    
    const  data  = await captcha({data:this.timestamp,sjc:this.sjc});
    console.log(data);
    console.log(data.timestamp);
    
    this.loginForm.checkKey = data.timestamp;
    this.captchaImage = data.result; 
    console.log('验证码:', this.loginForm.captcha);

 
  },
  },
  mounted() {
    this.getCode11();
  },
};
</script>

<style>
.el-input {
  margin-bottom: 20px;
}
</style>